<?php
class Category extends AppController{
	public function __construct(){
		parent::__construct();
	}
	public function index(){

	}
	public function view($id = null){
		if(empty($id))
			redirect('/');
			
		$cat = $this->getEM()->getRepository('\Entity\ShCategory')->findOneById($id);

		if(empty($cat))
			redirect('/');
			
		$sortBy = $this->input->get('sortBy');
		if(empty($sortBy))
			$sortBy = 'stt';
		$sortByDefault = array('name' => 'ten', 'id' => 'stt', 'price' => 'gia');
		
		$sortOrder = $this->input->get('sortOrder');
		if(empty($sortOrder))
			$sortOrder = 'desc';
		$sortOrderByDefault = array('desc' => 'desc', 'asc' => 'asc');
		
		$sortByVal = array_search($sortBy, $sortByDefault);
		if($sortByVal === false)
			$sortByVal = 'id';
		
		$sortOrderVal = array_search($sortOrder, $sortOrderByDefault);
		if($sortOrderVal === false)
			$sortOrderVal = 'desc';
			
		$pag= array(
			'uri_segment'=>4,
			'sort'=>false,
			'per_page'=>12,
			'sortList'=>array('stt'=>'id','Tên Loại'=>'name','Kích hoạt'=>'active','Sort'=>'sort'),
			'base_url'=>'/category/view/'.$id,
			'sortBy'=>$sortByVal,
			'sortOrder'=>$sortOrderVal,
			'get' => array(
				'sortBy'=>$sortBy,
				'sortOrder'=>$sortOrder
			),
		);
		
		$pag = $this->paginate($pag);
		$this->pagination->suffix = $pag['query'];
		$pag['total_rows'] = $this->getEM()->getRepository('Entity\ShCategory')->front_total_rows($cat->getId());
		
		$this->pagination->initialize($pag);
		$this->data['pagination_link'] = $this->pagination->create_links();
		$this->data['product'] = $this->getEM()->getRepository('\Entity\ShCategory')->frontPaginate($cat, $pag);
		
		$this->data['pag'] = $pag;
		$this->render('category/view');
	}
	public function admin_index($sortBy = 'id',$sortOrder = 'desc'){
		$pag= array(
			'uri_segment'=>6,
			'sort'=>true,
			'per_page'=>10,
			'sortList'=>array('stt'=>'id','Tên Loại'=>'name','Kích hoạt'=>'active','Sort'=>'sort'),
			'base_url'=>'/admin/category/index',
			'sortBy'=>$sortBy,
			'sortOrder'=>$sortOrder
			);
		$pag = $this->paginate($pag);
		$pag['total_rows'] = $this->getEM()->getRepository('Entity\ShCategory')->total_rows($pag);
		$this->pagination->initialize($pag);
		$this->data['pagination_link'] = $this->pagination->create_links();
		$this->data['cate'] = $this->getEM()->getRepository('\Entity\ShCategory')->backendPaginate($pag);
		$this->render('category/admin_index',array('back.js'));
	}
	public function mod_index($sortBy ='id',$sortOrder='desc'){
		$pag = array(
			'uri_segment'=>6,
			'sort'=>true,
			'per_page'=>10,
			'sortList'=>array('stt'=>'id','Tên Loại'=>'name','Kích hoạt'=>'active','Sort'=>'sort'),
			'base_url'=>'/admin/category/index',
			'sortBy'=>$sortBy,
			'sortOrder'=>$sortOrder
			);
		$pag = $this->paginate($pag);
		$pag['total_rows'] = $this->getEM()->getRepository('Entity\ShCategory')->total_rows($pag);
		$this->pagination->initialize($pag);
		$this->data['pagination_link'] = $this->pagination->create_links();
		$this->data['cate'] = $this->getEM()->getRepository('\Entity\ShCategory')->backendPaginate($pag);
		$this->render('category/mod_index',array('back.js'));
	}
	public function admin_add(){
		$this->cate_add();
		$this->data['breadcrumb'] = array(
			'Category' =>base_url('admin/category/index'),
			'Thêm Category' =>NULL
			);
		$this->render('category/admin_add',
			array('jquery.sceditor.bbcode.min','language/vi','sceditor','thumbnail-module'),
			array('default.min','tabs')
			);
	}
	public function mod_add(){
		$this->validation();
		if($this->input->post() && $this->form_validation->run('category_add')):
			if($this->getEM()->getRepository('Entity\ShCategory')->addCate($this->input)):
				$this->log(8,array($this->data['user']));
			$this->session->set_flashdata('success','Thêm Thành Công');
			redirect('/mod/category');
			else:
				$this->data['flash'] = array('error'=>'Lỗi Sql, Thử Lại');
			endif;
			endif;
			
		$this->data['breadcrumb'] = array(
			'Category' =>base_url('admin/category/index'),
			'Thêm Category' =>NULL
			);
		$this->render('category/mod_add',
			array('jquery.sceditor.bbcode.min','language/vi','sceditor','thumbnail-module'),
			array('default.min','tabs')
			);
	}
	private function cate_add(){
		$this->validation();
		if($this->input->post() && $this->form_validation->run('category_add')):
			if($this->getEM()->getRepository('Entity\ShCategory')->addCate($this->input)):
				$this->log(8,array($this->data['user']));
				$this->session->set_flashdata('success','Thêm Thành Công');
			redirect('/admin/category');
			else:
				$this->data['flash'] = array('error'=>'Lỗi sql, thử lại');
			endif;
		endif;
			return true;
	}
	public function admin_edit($id=NULL){
		$id = (int)$id;
		if(empty($id) || !is_int($id)):
			$this->session->set_flashdata('error','Sai mã Cate');
		redirect('/admin/category');
		endif;
		$this->cate_edit($id);
		$this->render('category/admin_edit');
	}
	public function mod_edit($id = NULL){
		$id = (int)$id;
		if(empty($id) || !is_int($id)):
			$this->session->set_flashdata('error','Sai mã Cate');
		redirect('/mod/category');
		endif;
		$repo = $this->getEM()->getRepository('Entity\ShCategory');
		$this->data['cate'] = $repo->findOneById($id);
		if(empty($this->data['cate'])):
			$this->session->set_flashdata('error','Không có Cate');
		redirect('/mod/category');
		endif;

		$this->validation();
		if($this->input->post() && $this->form_validation->run('category_update')):
			$cate= $this->getEM()->getRepository('Entity\ShCategory')->updateCate($this->input,$this->data['cate']);
		if($cate):
			$this->log(8,array($this->data['user']));
			$this->session->set_flashdata('success','Sửa Cate Thành Công');
		redirect('/mod/category');
		else:
			$this->data['flash']= array('error'=>'Lỗi sql, thử lại');
		endif;
		endif;
		$this->render('category/mod_edit');
	}
	private function cate_edit($id){
		$repo = $this->getEM()->getRepository('Entity\ShCategory');
		$this->data['cate'] = $repo->findOneById($id);
		if(empty($this->data['cate'])):
			$this->session->set_flashdata('error','Không có Cate');
		redirect('/admin/category');
		endif;

		$this->validation();
		if($this->input->post() && $this->form_validation->run('category_update')):
			$cate= $this->getEM()->getRepository('Entity\ShCategory')->updateCate($this->input,$this->data['cate']);
		if($cate):
			$this->log(8,array($this->data['user']));
			$this->session->set_flashdata('success','Sửa Cate Thành Công');
		redirect('/admin/category');
		else:
			$this->data['flash']= array('error'=>'Lỗi sql, thử lại');
		endif;
		endif;
	}
	public function admin_delete($id=NULL){
		$id = (int)$id;
		if(empty($id) || !is_int($id)):
			$this->session->set_flashdata('error','Sai mã cate');
		redirect('/admin/category');
		endif;

		$this->cate_delete($id);
		$this->data['breadcrumb'] = array(
			'Category'=>base_url('/admin/category'),
			'Xóa Category {'.$id.'}' => null
			);

		$this->data['breadcrumb']['ext']= array(
			'button' =>array('url'=>'category/add','name'=>'Thêm Cate')
			);
		$this->render('category/admin_delete');
	}
	public function mod_delete($id = NULL){
		$id = (int)$id;
		if(empty($id) || !is_int($id)):
			$this->session->set_flashdata('error','Sai mã cate');
		redirect('/mod/category');
		endif;

		$this->data['cate']=  $this->getEM()->getRepository('Entity\ShCategory')->findOneById($id);
		if(empty($this->data['cate'])):
			$this->session->set_flashdata('error','Không Có Cate');
		redirect('/'.$url.'/category');
		endif;

		$this->validation();
		if($this->input->post() && $this->form_validation->run('category_delete')):
			if($this->getEM()->getRepository('Entity\ShCategory')->deleteCate($this->data['cate'])):
				$this->log(5,array($this->data['user']));
				$this->session->set_flashdata('success','Xóa Thành Công');
			redirect('/mod/category');
			else:
				$this->data['flash']= array('error'=>'Lỗi sql, Thử lại');
			endif;
			endif;
		$this->data['breadcrumb'] = array(
			'Category'=>base_url('/admin/category'),
			'Xóa Category {'.$id.'}' => null
			);

		$this->data['breadcrumb']['ext']= array(
			'button' =>array('url'=>'category/add','name'=>'Thêm Cate')
			);
		$this->render('category/mod_delete');
	}
	private function cate_delete($id,$url='admin'){
		$this->data['cate']=  $this->getEM()->getRepository('Entity\ShCategory')->findOneById($id);
		if(empty($this->data['cate'])):
			$this->session->set_flashdata('error','Không Có Cate');
		redirect('/'.$url.'/category');
		endif;

		$this->validation();
		if($this->input->post() && $this->form_validation->run('category_delete')):
			if($this->getEM()->getRepository('Entity\ShCategory')->deleteCate($this->data['cate'])):
				$this->log(5,array($this->data['user']));
				$this->session->set_flashdata('success','Xóa Thành Công');
			redirect('/'.$url.'/category');
			else:
				$this->data['flash']= array('error'=>'Lỗi sql, Thử lại');
			endif;
			endif;
	}
	public function admin_shutdown($id){
		$id = (int)$id;
		if(empty($id) || !is_int($id)):
			$this->session->set_flashdata('error','Sai mã Cate');
		redirect('/admin/category');
		endif;

		$cate = $this->getEM()->getRepository('Entity\ShCategory')->findOneById($id);
		if(empty($cate)):
			$this->session->set_flashdata('error','Không có cate này');
		redirect('/admin/category');
		endif;

		$active = $cate->getActive();
		$cate->setActive(!$active);
		$this->getEM()->persist($cate);
		$this->getEM()->flush();
		redirect('/admin/category');
	}
	public function mod_shutdown($id){
		$id = (int)$id;
		if(empty($id) || !is_int($id)):
			$this->session->set_flashdata('error','Sai mã Cate');
		redirect('/mod/category');
		endif;

		$cate = $this->getEM()->getRepository('Entity\ShCategory')->findOneById($id);
		if(empty($cate)):
			$this->session->set_flashdata('error','Không có cate này');
		redirect('/mod/category');
		endif;

		$active = $cate->getActive();
		$cate->setActive(!$active);
		$this->getEM()->persist($cate);
		$this->getEM()->flush();
		redirect('/mod/category');
	}
}
?>